package com.company.class8;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Classname HomeWork
 * @Description 类型说明
 * @Date 2022/4/25 23:43
 * @Created by zbw
 */

public class HomeWork {
    public static void main(String[] args) {
        String s = "特斯汀学院负责讲授Java的老师是Roy，负责讲授Python的老师是Will，负责讲授性能的老师是土匪，负责讲授零基础的老师是掌心。";
        Pattern p = Pattern.compile("授(.*?)的老师是(.*?)[，。]");
        Matcher matcher = p.matcher(s);
        Map<String,String> map = new HashMap<>();
        while (matcher.find()){
            map.put(matcher.group(1),matcher.group(2));
        }
        System.out.println(map);

        String js = "{\"status\":{\"code\":\"0\",\"msg\":\"\"},\"data\":{\"user\":{\"user_id\":1457879378,\"user_sign\":\"2030043136 1457593567\",\"fdi_userNameMode\":0,\"src\":\"\",\"user_name\":\"\\u9632\\u8f90\\u5c04697\",\"is_login\":1,\"portrait\":\"5279e998b2e8be90e5b084363738e556\",\"cookie_sign\":null,\"bdstoken\":\"584a17b8ee631000\"},\"uinfo\":{\"md5\":\"2e5acb67f60ff784c85354a50cdae4c8\",\"data\":\"6c282cfeb129e22edac1e0ff38b9d2a7\"}}}";
//        System.out.println(js);
        //转成一个json对象类型的对象
        JSONObject jsonObject = JSON.parseObject(js);
//        System.out.println(jsonObject.get("data"));
        //获取data的值
        String data = jsonObject.get("data").toString();
//        System.out.println(data);
        JSONObject jsonData = JSON.parseObject(data);
        //定义一个map，key为String类型，value为Map类型
        Map<String,Map<String,String>> keyMap = new HashMap<>();

        for(String keys: jsonData.keySet()){
//            System.out.println(keys+":"+jsonData.get(keys));
            //存储子json内容的map
            Map<String,String> dataMap = new HashMap<>();
            JSONObject keyData = JSON.parseObject(jsonData.get(keys).toString());
            for(String key: keyData.keySet()){
//                System.out.println(key+":"+keyData.get(key).toString());
                dataMap.put(key,keyData.get(key).toString());

            }
            keyMap.put(keys,dataMap);

        }
        System.out.println("JSON转Map格式"+keyMap);
        String value = JSONObject.toJSONString(keyMap);
        System.out.println("map转String格式："+value);
        JSONObject valueJson = JSON.parseObject(value);
        System.out.println("String转JSON格式："+value);



    }
}
